package com.yehaikeji.workflow.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.util.StringUtil;
import com.yehaikeji.workflow.bean.CountBean;
import com.yehaikeji.workflow.bean.Excelthing;
import com.yehaikeji.workflow.bean.incident;
import com.yehaikeji.workflow.bean.thing;
import com.yehaikeji.workflow.bean.unit;
import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.mapper.incidentMapper;
import com.yehaikeji.workflow.mapper.thingMapper;
import com.yehaikeji.workflow.mapper.unitMapper;
import com.yehaikeji.workflow.mapper.userMapper;
import com.yehaikeji.workflow.service.thingService;
import com.yehaikeji.workflow.util.formatVerify;

@Service
public class thingServiceImpl implements thingService {
	@Autowired
	private thingMapper thingMapper;
	@Autowired
	private userMapper userMapper;
	@Autowired
	private incidentMapper incidentMapper;
	@Autowired
	private unitMapper unitMapper;

	@Override
	public String inserthing(thing thing, Date createdate, String sendName, String accpetName, String sendUnitid,
			String accptUnitid, String unitidc) {
		if (thing.getNumber() <= 0) {
			throw new RuntimeException("数字错误n");
		} else if (thing.getTypeid() <= 0 || thing.getTypeid() > 4) {
			throw new RuntimeException("类型错误t");
		}
		String thingid = UUID.randomUUID().toString().replaceAll("-", "");

		if (thing.getTypeid() == 1 || thing.getTypeid() == 4) {
			if (StringUtil.isEmpty(thing.getBphone())) {
				throw new RuntimeException("办理者电话为空");
			}
			if (!formatVerify.isMobileNO(thing.getBphone())) {
				throw new RuntimeException("办理者电话错误");
			}
			if (StringUtil.isEmpty(thing.getBname())) {
				throw new RuntimeException("办理者姓名为空");
			}
			thing.setTypename("代办");
		} else {
			thing.setBname(null);
			thing.setBphone(null);
			if (thing.getTypeid() == 2) {
				thing.setTypename("代收");
			} else if (thing.getTypeid() == 3) {
				thing.setTypename("代发");
			} else {
				throw new RuntimeException("类型错误");
			}
		}

		unit unit = unitMapper.selectByPrimaryKey(sendUnitid);
		unit unit2 = unitMapper.selectByPrimaryKey(accptUnitid);
		unit unit3 = unitMapper.selectByPrimaryKey(unitidc);
		if (unit == null || unit2 == null || unit3 == null) {
			throw new RuntimeException("部门参数错误");
		}
		user user = userMapper.selectByPrimaryKey(thing.getUserid());
		// if (user == null) {
		// throw new RuntimeException("userid错误");
		// }
		thing.setCreatedate(new Date());
		thing.setThingid(thingid);

		incident incident = new incident();
		incident.setThingid(thingid);
		if (thing.getTypeid() == 3) {
			incident.setStatus(8);
			thing.setStatus(8);
		} else {
			incident.setStatus(1);
			thing.setStatus(1);
		}
		incident.setUnitidc(unitidc);
		thingMapper.insertSelective(thing);

		String incidentid = UUID.randomUUID().toString().replaceAll("-", "");
		incident.setIncidentid(incidentid);

		incident.setSendname(sendName);
		incident.setAcceptname(accpetName);

		incident.setUnitida(sendUnitid);
		incident.setUnitidb(accptUnitid);
		incident.setSendunit(unit.getName());
		incident.setAccepetunit(unit2.getName());
		incident.setSnapinunit(unit3.getName());

		incident.setCreatedate(createdate);
		incident.setUserid(thing.getUserid());
		incidentMapper.insertSelective(incident);

		return incidentid;
	}

	@Override
	public List<thing> selectlistpage(String bname, Integer status, Integer typeid, String bphone, String thingname,
			String startDate, String endDate, String unitida, String unitidc) {
		Map<String, Object> map = new HashMap<>();

		map.put("bname", bname);

		if (status == null) {
			// map.put("list", null);
		} else {
			// 办结
			if (status == -1) {
				List<Integer> integers = new ArrayList<>();
				integers.add(3);
				integers.add(5);
				integers.add(7);
				integers.add(9);
				map.put("erer", "1");
				map.put("list", integers);

			} else if (status == -2) {
				// 未办结
				List<Integer> integers = new ArrayList<>();
				integers.add(1);
				integers.add(4);
				integers.add(6);
				integers.add(8);
				map.put("erer", "0");
				map.put("list", integers);
			} else if (status > 0) {
				// 某个具体状态
				List<Integer> integers = new ArrayList<>();
				integers.add(status);
				map.put("list", integers);
			}
		}
		map.put("typeid", typeid);
		map.put("bphone", bphone);
		map.put("name", thingname);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("unitida", unitida);
		map.put("unitidc", unitidc);

		return thingMapper.selectfiltratelist(map);
	}

	@Override
	public int deletethingthis(String thingid) {
		return thingMapper.deleteByPrimaryKey(thingid);
	}

	@Override
	public String inserthingok(thing thing, Date createdate, String sendName, String accpetName, String sendUnitid,
			String accpetUnitid) {
		if (thing.getNumber() <= 0) {
			throw new RuntimeException("数字错误n");
		} else if (thing.getTypeid() <= 0 || thing.getTypeid() > 4) {
			throw new RuntimeException("类型错误t");
		}
		String thingid = UUID.randomUUID().toString().replaceAll("-", "");

		if (thing.getTypeid() == 1 || thing.getTypeid() == 4) {
			if (StringUtil.isEmpty(thing.getBphone()) || !formatVerify.isMobileNO(thing.getBphone())
					|| StringUtil.isEmpty(thing.getBname())) {
				throw new RuntimeException("办理者信息错误b");
			}
			thing.setTypename("代办");
		} else {
			thing.setBname(null);
			thing.setBphone(null);
			if (thing.getTypeid() == 2) {
				thing.setTypename("代收");
			} else if (thing.getTypeid() == 3) {
				thing.setTypename("代发");
			} else {
				throw new RuntimeException("类型错误");
			}
		}

		unit unit = unitMapper.selectByPrimaryKey(sendUnitid);
		unit unit2 = unitMapper.selectByPrimaryKey(accpetUnitid);
		if (unit == null || unit2 == null) {
			throw new RuntimeException("部门参数错误");
		}
		user user = userMapper.selectByPrimaryKey(thing.getUserid());
		// if (user == null) {
		// throw new RuntimeException("userid错误");
		// }
		thing.setCreatedate(new Date());
		thing.setThingid(thingid);

		incident incident = new incident();
		incident.setThingid(thingid);
		if (thing.getTypeid() == 3) {
			incident.setStatus(8);
			thing.setStatus(8);
		} else {
			incident.setStatus(1);
			thing.setStatus(1);
		}

		thingMapper.insertSelective(thing);

		String incidentid = UUID.randomUUID().toString().replaceAll("-", "");
		incident.setIncidentid(incidentid);

		incident.setSendname(sendName);
		incident.setAcceptname(accpetName);

		incident.setUnitida(sendUnitid);
		incident.setUnitidb(accpetUnitid);
		incident.setSendunit(unit.getName());
		incident.setAccepetunit(unit2.getName());

		incident.setCreatedate(createdate);
		incident.setUserid(thing.getUserid());
		incidentMapper.insertSelective(incident);

		return incidentid;
	}

	@Override
	public List<Excelthing> selectDownloadPage(List<String> list) {

		List<thing> list2 = thingMapper.selectDownloadList(list);
		List<Excelthing> list3 = new ArrayList<>();

		for (thing thing : list2) {
			Excelthing excelthing = new Excelthing();
			excelthing.setThing(thing);

			Map<String, Object> map = new HashMap<>();
			map.put("thingid", thing.getThingid());
			map.put("status", thing.getStatus());
			incidentMapper.deleteuselessincident(map);

			List<incident> list4 = incidentMapper.selectBythingidhistory(thing.getThingid());
			excelthing.setList(list4);
			list3.add(excelthing);
		}

		return list3;
	}

	@Override
	public List<CountBean> selectCount(String startDate, String endDate, int typeid) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		paramMap.put("typeid", typeid);
		List<CountBean> list = thingMapper.selectCountByDate(paramMap);

		List<CountBean> returnList = thingMapper.selectCountByDateall(paramMap);
		returnList.addAll(list);

		return returnList;
	}

	@Override
	public Map<String, Object> getrecordlisthistory(String thingid) {
		Map<String, Object> map = new HashMap<>();
		map.put("thing", thingMapper.selectByPrimaryKey(thingid));
		List<incident> list = incidentMapper.selectBythingidhistory(thingid);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for (incident incident : list) {
			incident.setCreatedateS(dateFormat.format(incident.getCreatedate()));
			// if(incident.getTime()==null) {
			// incident.setTimeS("");
			// }else {
			// incident.setTimeS(dateFormat.format(incident.getTime()));
			// }
		}
		map.put("list", list);
		return map;
	}

	@Override
	public List<thing> countselectlist(Integer typeid, String startDate, String endDate, String unitida,
			Integer classify) {
		Map<String, Object> map = new HashMap<>();
		map.put("typeid", typeid);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("unitida", unitida);
		List<Integer> list = new ArrayList<>();
		List<String> list2 = new ArrayList<>();
		if (classify == 1) {
			// map.put("list",null);
			// map.put("list2",null);
		} else if (classify == 2) {
			list.add(1);
			list.add(4);
			list.add(6);
			list.add(8);
			map.put("list", list);
			map.put("erer", 0);
		} else if (classify == 3) {
			list.add(3);
			list.add(5);
			list.add(7);
			list.add(9);
			map.put("list", list);
			map.put("erer", 1);
		} else {
			list.add(3);
			list.add(5);
			list.add(7);
			list.add(9);
			map.put("list", list);
			map.put("erer", 1);
			if (classify == 4) {
				list2.add("10");
				list2.add("9");
				list2.add("8");
				list2.add("7");
				map.put("list2", list2);
			} else if (classify == 5) {
				list2.add("6");
				list2.add("5");
				list2.add("4");
				map.put("list2", list2);
			} else if (classify == 6) {
				list2.add("1");
				list2.add("2");
				list2.add("3");
				map.put("list2", list2);
			}else if (classify == 6) {
				List<String> list3=new ArrayList<>();
				for (int i = 1; i <= 10; i++) {
					list3.add(""+i);
				}
				map.put("list3", list3);
			}
		}
		
		return thingMapper.countselectlist(map);
	}
}
